#!/bin/bash

T90=( \
  6.314 2.920 2.353 2.132 2.015 1.943 1.895 1.860 1.833 1.812 \
  1.796 1.782 1.771 1.761 1.753 1.746 1.740 1.734 1.729 1.725 \
  1.721 1.717 1.714 1.711 1.708 1.706 1.703 1.701 1.699 1.697 \
  )

T95=( \
  12.71 4.303 3.182 2.776 2.571 2.447 2.365 2.306 2.262 2.228 \
  2.201 2.179 2.160 2.145 2.131 2.120 2.110 2.101 2.093 2.086 \
  2.080 2.074 2.069 2.064 2.060 2.056 2.052 2.048 2.045 2.042 \
  )

TIMES[0]=$($@ | grep Mean | awk '{ print $5 }')
SUM=${TIMES[0]}
printf '#%-2d %.2f\n' 1 ${TIMES[0]}

i=1
while true; do
  TIME=$($@ | grep Mean | awk '{ print $5 }')
  TIMES[$i]=$TIME
  SUM=$(echo "$SUM + $TIME" | bc)
  N=$(($i + 1))

  MEAN=$(echo "$SUM / $N" | bc -l)

  STD=0
  for j in "${TIMES[@]}"; do
    STD=$(echo "($j - $MEAN) ^ 2 + $STD" | bc -l)
  done
  STD=$(echo "sqrt ($STD / $i)" | bc -l)

  ERR=$(echo "${T95[$i]} * $STD / sqrt($N)" | bc -l)
  PRECISION=$(echo "$ERR / $MEAN" | bc -l)

  printf '#%-2d %.2f %.2f %.4f %.2f %.3f\n' \
    $N $TIME $MEAN $STD $ERR $PRECISION

  if (($N >= 10 || $N >= 5 && $(echo "$PRECISION < 0.02" | bc) == 1)); then
    break
  else
    i=$N
  fi
done

